﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;

using WebAutomation.Browsers;

namespace WebAutomation
{
    public class VariableCommand : CommandBase
    {
        public VariableCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }

        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref System.Data.DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            string command = parameter[0];
            try
            {
                testLineResult.LineCommand = command;
                testLineResult.LineParameter = parameter[1];
                if (this.BrowserObject == null)
                {
                    this.BrowserObject = browserObject;
                }
                string varName = "";
                string varValue = "";

                switch (command.ToLower())
                {
                    case "set":
                        parameter[1] = parameter[1].Replace(@":\", @"~\");
                        
                        varName = parameter[1].Split(':')[0];
                        if (_isDateTimeVariable(parameter[1]))
                        {
                            string newVarValue=parameter[1].Replace(varName,"").Replace(@":", @"!");
                            parameter[1] = varName + ":" + newVarValue.Replace(":!",":");
                            parameter[1] = parameter[1].Replace(":!", ":");
                        }

                        varValue = parameter[1].Split(':')[1];
                        varValue = varValue.Replace(@"~\", @":\");
//                        varValue = varValue.Replace(@"!", @":");
                        if (varValue.Contains("Excel["))
                        {
                            int excelColumnIndex = Convert.ToInt32(varValue.Replace("Excel[", "").Replace("]", ""));
                            varValue = TestFileReader.GetExcelColumnData(excelColumnIndex);

                        }
                        SetVariable(varName, varValue);
                        testLineResult.LinePassed = true;
                        break;
                   
                }
              
            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }

            return testLineResult.LinePassed;
        }

        public virtual void SetVariable(string varName,string varValue)
        {
            TestCommands.SetVariable(varName, varValue);

        }

        private bool _isDateTimeVariable(string varValue)
        {

            bool returnValue = varValue.ToLower().Contains("jan")
                               ||varValue.ToLower().Contains("january")
                               ||varValue.ToLower().Contains("feb")
                               ||varValue.ToLower().Contains("february")
                               ||varValue.ToLower().Contains("mar")
                               ||varValue.ToLower().Contains("march")
                               ||varValue.ToLower().Contains("apr")
                               ||varValue.ToLower().Contains("april")
                               ||varValue.ToLower().Contains("may")
                               ||varValue.ToLower().Contains("jun")
                               ||varValue.ToLower().Contains("june")
                               ||varValue.ToLower().Contains("jul")
                               ||varValue.ToLower().Contains("july")
                               ||varValue.ToLower().Contains("aug")
                               ||varValue.ToLower().Contains("august")
                               ||varValue.ToLower().Contains("sep")
                               ||varValue.ToLower().Contains("september")
                               ||varValue.ToLower().Contains("oct")
                               ||varValue.ToLower().Contains("october")
                               ||varValue.ToLower().Contains("nov")
                               ||varValue.ToLower().Contains("november")
                               ||varValue.ToLower().Contains("dec")
                               ||varValue.ToLower().Contains("december");

            return returnValue;
        }
    }
}
